//antiCheat 反修改作弊int
export class ObscuredInt {
    private static cryptoKey: number = 444444;
    private currentCryptoKey: number;
    private hiddenValue: number;
    private inited: boolean;
    private fakeValue: number;
    private fakeValueActive: boolean;

    constructor(value: number) {
        this.currentCryptoKey = ObscuredInt.cryptoKey;
        this.hiddenValue = this.encrypt(value);
        this.fakeValue = value;
        this.fakeValueActive = true;
        this.inited = true;
    }

    public static setNewCryptoKey(newKey: number): void {
        ObscuredInt.cryptoKey = newKey;
    }

    public static encrypt(value: number, key: number = 0): number {
        if (key === 0) {
            return value ^ ObscuredInt.cryptoKey;
        }
        return value ^ key;
    }

    public static decrypt(value: number, key: number = 0): number {
        if (key === 0) {
            return value ^ ObscuredInt.cryptoKey;
        }
        return value ^ key;
    }

    public static fromEncrypted(encrypted: number): ObscuredInt {
        let instance = new ObscuredInt(0);
        instance.setEncrypted(encrypted);
        return instance;
    }

    public applyNewCryptoKey(): void {
        if (this.currentCryptoKey!== ObscuredInt.cryptoKey) {
            this.hiddenValue = this.encrypt(this.decrypt());
            this.currentCryptoKey = ObscuredInt.cryptoKey;
        }
    }

    public randomizeCryptoKey(): void {
        this.hiddenValue = this.decrypt();
        this.currentCryptoKey = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
        this.hiddenValue = this.encrypt(this.hiddenValue);
    }

    public getEncrypted(): number {
        this.applyNewCryptoKey();
        return this.hiddenValue;
    }

    public setEncrypted(encrypted: number): void {
        this.inited = true;
        this.hiddenValue = encrypted;
        if (this.currentCryptoKey === 0) {
            this.currentCryptoKey = ObscuredInt.cryptoKey;
        }
        if (this.isCheatingDetectorRunning()) {
            this.fakeValueActive = false;
            this.fakeValue = this.decrypt();
            this.fakeValueActive = true;
        } else {
            this.fakeValueActive = false;
        }
    }

    public getDecrypted(): number {
        return this.decrypt();
    }

    private decrypt(): number {
        if (!this.inited) {
            this.currentCryptoKey = ObscuredInt.cryptoKey;
            this.hiddenValue = this.encrypt(0);
            this.fakeValue = 0;
            this.fakeValueActive = false;
            this.inited = true;
            return 0;
        }
        let decrypted = ObscuredInt.decrypt(this.hiddenValue, this.currentCryptoKey);
        if (this.isCheatingDetectorRunning() && this.fakeValueActive && decrypted!== this.fakeValue) {
            this.triggerCheatingDetected();
        }
        return decrypted;
    }

    // 假设这里有检测作弊检测器是否运行的方法，需根据实际情况实现
    private isCheatingDetectorRunning(): boolean {
        // 返回作弊检测器运行状态，暂时返回 false
        return false;
    }

    // 假设这里有触发作弊检测的方法，需根据实际情况实现
    private triggerCheatingDetected(): void {
        console.log("作弊检测被触发");
    }

    private encrypt(value: number): number {
        return ObscuredInt.encrypt(value, this.currentCryptoKey);
    }
}

// 以下是类型转换和操作符重载的实现
// 隐式转换为 number 类型
function toNumber(obscuredInt: ObscuredInt): number {
    return obscuredInt.getDecrypted();
}

